//会员插件ctrl-shift和框选start
let selectStartIndex = null
let selectionStartIndex = null
let isSelecting = false
export function addSelectedKey (data) {
  return data.map(i=> {
    i.checked = false
    return i
  })
}
export const selectListItem = (item, event, leftList)=> {
	if (event.ctrlKey || event.metaKey) {
		// Ctrl 或 Command 键选择
		item.checked = !item.checked;
	} else if (event.shiftKey) {
		// Shift 键选择范围
		if (selectionStartIndex !== null) {
			const startIndex = leftList.findIndex(i => i.id === selectionStartIndex);
			const endIndex = leftList.findIndex(i => i.id === item.id);
			leftList.forEach((i, index) => {
				if (index >= Math.min(startIndex, endIndex) && index <= Math.max(startIndex, endIndex)) {
					i.checked = true;
				}
			});
		}
	} else {
		// 单独选择
		leftList.forEach(i => i.checked = false);
		item.checked = true;
		selectionStartIndex = item.id;
	}
}
export const startSelection = (event, index, leftList)=> {
	event.preventDefault()
	selectStartIndex = index
	if (!event.ctrlKey && !event.shiftKey) {
		isSelecting = true;
		selectionStartIndex = leftList.find(i => i.checked)?.id;
		selectRange(index, index, leftList);
	}
}
export const endSelection = ()=> {
	
	isSelecting = false;
}
export const continueSelection = (event, index, leftList)=> {
	event.preventDefault()
	
	if (isSelecting) {
		
		selectRange(selectStartIndex, index, leftList)
	}
}
const selectRange = (startIndex, endIndex, leftList)=> {
	const start = Math.min(startIndex, endIndex);
	const end = Math.max(startIndex, endIndex);
	
	
	leftList.forEach((item, index) => {
		item.checked = index >= start && index <= end ? true : item.checked
	})
}
// 会员插件ctrl-shift和框选end

//深拷贝
export function deepClone(source) {
  if (!source && typeof source !== 'object') {
    throw new Error('error arguments', 'deepClone')
  }
  const targetObj = source.constructor === Array ? [] : {}
  Object.keys(source).forEach(keys => {
    if (source[keys] && typeof source[keys] === 'object') {
      targetObj[keys] = deepClone(source[keys])
    } else {
      targetObj[keys] = source[keys]
    }
  })
  return targetObj
}

